package org.sbbs.app.license.service.impl;
import java.io.UnsupportedEncodingException;


public class CNByteTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String cstr="他妈的真不是个东西Rdfafasdfdadf去你的.";
		int a = cstr.length();
		byte[] cbyte=null;
		try {
			cbyte = cstr.getBytes("GBK");
			
		
		System.out.println(toHexString(cbyte));
		System.out.println(toHexStringOnly(cbyte));
		System.out.println(gbEncoding(cstr));
		String bstr=new   String(cbyte, "GBK");
		int b=bstr.length();
		String str=bstr.trim();
			System.out.println(str);
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
//		Map chMap=Charset.availableCharsets();
//		Set chKeys=chMap.keySet();
//		Iterator itr = chKeys.iterator();
//		while(itr.hasNext()){
//			System.out.println(chMap.get(itr.next()));
//		}


	}
	public static String toHexString(byte[] block) {
		StringBuffer buf = new StringBuffer();

		int len = block.length;

		for (int i = 0; i < len; i++) {
			byte2hex(block[i], buf);
			if (i < len - 1) {
				buf.append(":");
			}
		}
		return buf.toString();
	}
	public static String toHexStringOnly(byte[] block) {
		StringBuffer buf = new StringBuffer();

		int len = block.length;

		for (int i = 0; i < len; i++) {
			byte2hex(block[i], buf);
//			if (i < len - 1) {
//				buf.append(":");
//			}
		}
		return buf.toString();
	}
	public static void byte2hex(byte b, StringBuffer buf) {
		char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		int high = ((b & 0xf0) >> 4);
		int low = (b & 0x0f);
		buf.append(hexChars[high]);
		buf.append(hexChars[low]);
	}
	public static String gbEncoding(final String gbString) {
		char[] utfBytes = gbString.toCharArray();
		String unicodeBytes = "";
		for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
			String hexB = Integer.toHexString(utfBytes[byteIndex]);
			if (hexB.length() <= 2) {
				hexB = "00" + hexB;
			}
			// unicodeBytes = unicodeBytes + "\\\\u" + hexB;
			unicodeBytes = unicodeBytes + "\\u" + hexB;
		}
		// System.out.println("unicodeBytes is: " + unicodeBytes);
		return unicodeBytes;
	}
	
}
